//////////////////////////////////////////////////////////////////////////
#include <RocketLua/RocketLua.hpp>


using namespace Rocket::Core::Lua;


std::string LuaElement::Id( const Rocket::Core::Element* el )
{
	return el->GetId().CString();
}


void LuaElement::Id( Rocket::Core::Element* el, const std::string& s )
{
	el->SetId( Rocket::Core::String( s.c_str() ) );
}


int LuaElement::GetAttribute( lua_State* L, Rocket::Core::Element* el )
{
	// Need to implement Variant.
	return 0;
}


int LuaElement::HasAttribute( lua_State* L, Rocket::Core::Element* el )
{
	int	result	=	0;
	if( lua_isstring( L, -1 ) )
	{
		lua_pushboolean( L, el->HasAttribute( lua_tostring( L, -1 ) )==true );
		result	=	1;
	}
	return result;
}


int LuaElement::RemoveAttribute( lua_State* L, Rocket::Core::Element* el )
{
	if( lua_isstring( L, -1 ) )
		el->RemoveAttribute( lua_tostring( L, -1 ) );
	return 0;
}


int LuaElement::SetProperty( lua_State* L, Rocket::Core::Element* el )
{
	if( lua_isstring( L, -1 ) && lua_isstring( L, -2 ) )
	{
		el->SetProperty( lua_tostring( L, -2 ), lua_tostring( L, -1 ) );
	}

	return 0;
}


int LuaElement::SetInnerRml( lua_State* L, Rocket::Core::Element* el )
{
	if( lua_isstring( L, -1 ) )
	{
		el->SetInnerRML( lua_tostring( L, -1 ) );
	}
	return 0;
}


// TODO: Consider how to deal with rocket string.
// ***	Option 1:	Hack LuaBridge to support a generic wrapper function.  <DONE>
//	Option 2:	Try to manually hook in the wrappers after luabridge is done.
//	Option 3:	Get annoyed with these wrappers and piss and moan.. :)
void LuaElement::Register( lua_State* state )
{
	luabridge::getGlobalNamespace( state )
		.beginClass< Rocket::Core::Element >( "Element" )
		.addConstructor< void (*)( const Rocket::Core::String& ) >()
		.addFunction( "Blur", &Rocket::Core::Element::Blur )
		.addFunction( "Click", &Rocket::Core::Element::Click )
		.addFunction( "Focus", &Rocket::Core::Element::Focus )
		.addProperty( "Id", &LuaElement::Id, &LuaElement::Id )

		// Local static helper types.  If any of these fail, my hackery pokery
		// of LuaBridge probably failed.  Note: I didn't support "const" versions
		// of the type as it seems completely unneeded.  Though, probably it
		// should be supported.
		.addCFunction< LuaElement >( "GetAttribute", &LuaElement::GetAttribute )
		.addCFunction< LuaElement >( "HasAttribute", &LuaElement::HasAttribute )
		.addCFunction< LuaElement >( "RemoveAttribute", &LuaElement::RemoveAttribute )
		.addCFunction< LuaElement >( "SetProperty", &LuaElement::SetProperty )
		.addCFunction< LuaElement >( "SetInnerRml", &LuaElement::SetInnerRml )
		.endClass();
}
